package cn.iocoder.yudao.module.system.controller.admin.mini.other.turnTable.controller.admin.turntable;

import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.turnInfo.dal.dataobject.turninfo.TurnInfoDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.turnInfo.dal.mysql.turninfo.TurnInfoMapper;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.turnTable.controller.admin.turntable.vo.TurntableCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.turnTable.controller.admin.turntable.vo.TurntablePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.turnTable.controller.admin.turntable.vo.TurntableRespVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.turnTable.controller.admin.turntable.vo.TurntableUpdateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.turnTable.dal.dataobject.turntable.TurntableDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.turnTable.dal.mysql.turntable.TurntableMapper;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.turnTable.service.turntable.TurntableService;
import cn.iocoder.yudao.module.system.controller.admin.mini.userInfo.user.dal.dataobject.user.UserDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.userInfo.user.dal.mysql.user.QdUserMapper;
import lombok.Data;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 转盘管理")
@RestController
@RequestMapping("/qd/turntable")
@Validated
public class TurntableController {

    @Resource
    private TurntableService turntableService;
    @Resource
    TurntableMapper turntableMapper;
    @Resource
    TurnInfoMapper turnInfoMapper;
    @Resource
    QdUserMapper userMapper;

    @Operation(summary = "发送给用户转盘")
    @PostMapping("toUser")
    public CommonResult toUser(@RequestBody TurnTableUser vo){
        TurntableDO turntableDO = turntableMapper.selectById(vo.getId());
        if (turntableDO==null)
            throw new ServiceException(500,"轮盘不存在");
        if (vo.getIds()==null||vo.getIds().size()==0)
            throw new ServiceException(500,"请选择用户");

        //发放次数
        int size = vo.getIds().size();
        if (size>turntableDO.getSize())
            throw new ServiceException(500,"轮盘发放次数不足");

        List<TurnInfoDO> list=new ArrayList<>();
        for (Integer uid : vo.getIds()) {
            UserDO userDO = userMapper.selectById(uid);
            if (userDO==null)
                throw new ServiceException(500,"用户id"+uid+"不存在");
            list.add(new TurnInfoDO(turntableDO,userDO));
        }
        turnInfoMapper.insertBatch(list);

        turntableDO.setSize(turntableDO.getSize()-size);
        turntableMapper.updateById(turntableDO);
        return success("");
    }

    @PostMapping("/create")
    @Operation(summary = "创建转盘管理")
    @PreAuthorize("@ss.hasPermission('qd:turntable:create')")
    public CommonResult<Integer> createTurntable(@Valid @RequestBody TurntableDO createReqVO) {
        return success(turntableService.createTurntable(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新转盘管理")
    @PreAuthorize("@ss.hasPermission('qd:turntable:update')")
    public CommonResult<Boolean> updateTurntable(@Valid @RequestBody TurntableDO updateReqVO) {
        turntableService.updateTurntable(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除转盘管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qd:turntable:delete')")
    public CommonResult<Boolean> deleteTurntable(@RequestParam("id") Integer id) {
        turntableService.deleteTurntable(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得转盘管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qd:turntable:query')")
    public CommonResult  getTurntable(@RequestParam("id") Integer id) {
        return success(turntableService.getTurntable(id));
    }

    @GetMapping("/list")
    @Operation(summary = "获得转盘管理列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('qd:turntable:query')")
    public CommonResult  getTurntableList(@RequestParam("ids") Collection<Integer> ids) {
        return success(turntableService.getTurntableList(ids));
    }

    @GetMapping("/page")
    @Operation(summary = "获得转盘管理分页")
    @PreAuthorize("@ss.hasPermission('qd:turntable:query')")
    public CommonResult  getTurntablePage(@Valid TurntablePageReqVO pageVO) {
        return success(turntableService.getTurntablePage(pageVO));
    }

//    @GetMapping("/export-excel")
//    @Operation(summary = "导出转盘管理 Excel")
//    @PreAuthorize("@ss.hasPermission('qd:turntable:export')")
//    @OperateLog(type = EXPORT)
//    public void exportTurntableExcel(@Valid TurntableExportReqVO exportReqVO,
//              HttpServletResponse response) throws IOException {
//        List<TurntableDO> list = turntableService.getTurntableList(exportReqVO);
//        // 导出 Excel
//        List<TurntableExcelVO> datas = TurntableConvert.INSTANCE.convertList02(list);
//        ExcelUtils.write(response, "转盘管理.xls", "数据", TurntableExcelVO.class, datas);
//    }

}
@Data
class TurnTableUser{
    //用户id
    List<Integer> ids;
    //轮盘id
    Integer id;
}